package com.by.basicweb.controller;

import cn.hutool.core.net.Ipv4Util;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import com.alibaba.cloud.nacos.registry.NacosServiceRegistry;
import com.by.toolkit.model.Resp;
import com.by.toolkit.utils.IpUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

import static com.by.toolkit.utils.ExceptionUtil.getStackTrace;

@Slf4j
@RequestMapping
@RestController
public class NacosRegisterController {

    /**
     * nacos反向注册
     */
    @GetMapping("/nacosDeregister")
    public Resp nacosDeregister(NacosDeregisterParam param, HttpServletRequest httpServletRequest) {
        try {
            String ipV4 = IpUtil.getRemoteIpV4(httpServletRequest);
            if (!Ipv4Util.isInnerIP(ipV4)) {
                log.error("[NacosRegisterController][nacosDeregister] nacosDeregister request url no permission:{}", ipV4);
                return Resp.sysError();
            }
            NacosServiceRegistry nacosServiceRegistry = SpringUtil.getBean(NacosServiceRegistry.class);
            if (Objects.isNull(nacosServiceRegistry)) {
                log.error("[NacosRegisterController][nacosDeregister] nacosServiceRegistry not exist");
                return Resp.sysError();
            }
            NacosRegistration nacosRegistration = SpringUtil.getBean(NacosRegistration.class);
            if (Objects.isNull(nacosRegistration)) {
                log.error("[NacosRegisterController][nacosDeregister] nacosRegistration not exist");
                return Resp.sysError();
            }
            nacosServiceRegistry.deregister(nacosRegistration);
            log.info("[NacosRegisterController][nacosDeregister] ");
        }catch (Exception e){
            log.error("[NacosRegisterController][nacosDeregister] nacosDeregister fail exception:{}", getStackTrace(e));
        }
        return Resp.ok();
    }

    @GetMapping("/nacosRegister")
    public Resp nacosRegister(NacosDeregisterParam param, HttpServletRequest httpServletRequest) {

        try{
            String ipV4 = IpUtil.getRemoteIpV4(httpServletRequest);
            if (!Ipv4Util.isInnerIP(ipV4)) {
                log.error("[NacosRegisterController][nacosRegister] nacosDeregister request url no permission:{}", ipV4);
                return Resp.sysError();
            }
            NacosServiceRegistry nacosServiceRegistry = SpringUtil.getBean(NacosServiceRegistry.class);
            if (Objects.isNull(nacosServiceRegistry)) {
                log.error("[NacosRegisterController][nacosRegister] nacosServiceRegistry not exist");
                return Resp.sysError();
            }
            NacosRegistration nacosRegistration = SpringUtil.getBean(NacosRegistration.class);
            if (Objects.isNull(nacosRegistration)) {
                log.error("[NacosRegisterController][nacosRegister] nacosRegistration not exist");
                return Resp.sysError();
            }
            nacosServiceRegistry.register(nacosRegistration);
            log.info("[NacosRegisterController][nacosRegister] ");

        }catch (Exception e){
            log.error("[NacosRegisterController][nacosDeregister] nacosRegister fail exception:{}", getStackTrace(e));
        }
        return Resp.ok();
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    private static class NacosDeregisterParam {

        private String name;

    }
}
